package org.smartercontext.remotedeployable;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.smartercontext.contextmger.ciphermodule.CipherAES;
import org.smartercontext.contextmger.communication.Communication_Impl;
import org.smartercontext.contextmger.communication.Communication_ImplProxy;
import org.smartercontext.contextmger.json.writer.IJSONWriter;
import org.smartercontext.contextmger.json.writer.JSONWriter;

/**
 * Servlet implementation class LoginRemoteSC
 */
@WebServlet("/LoginRemoteSC")
public class LoginRemoteSC extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginRemoteSC() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	
    	try{
    	
    	if(request.getQueryString()!=null){
    		
    		String username = request.getParameter("username").toString();
    		String password = request.getParameter("password").toString();
    		
    		//System.out.println(username+password);
    		
    		if(username!=null&&password!=null){
    			if(username.length()<16&&password.length()<16){
    				
    				CipherAES caes = new CipherAES();
    				String usernameHex = caes.stringToHex(username);
    				String usenameCipher = caes.encrypt_decrypt(usernameHex, true);
    				
    				String passwordHex = caes.stringToHex(password);
    				String passwordCipher = caes.encrypt_decrypt(passwordHex, true);
    				
    				IJSONWriter jsonWritter = new JSONWriter();
    				String jFormat=jsonWritter.writeLogin(usenameCipher, passwordCipher);
    				
    				Communication_Impl comImpl = new Communication_ImplProxy();
    				boolean success = comImpl.login(jFormat, "json");
    				
    				if(success){
    					HttpSession session = request.getSession(true);
    					session.setAttribute("username", username);
    					response.sendRedirect("menu.jsp");
    				}else{
    					response.sendRedirect("index.jsp?Error=0");
    				}
    				
    			}else{
    				response.sendRedirect("index.jsp?Error=1");
    			}
    		}else{
    			response.sendRedirect("index.jsp?Error=2");
    		}
    		
    	}else{
    		response.sendRedirect("index.jsp?Error=2");
    	}
    	
    	}catch(Exception e){
    		response.sendRedirect("index.jsp?Error=3");
    	}
    	
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

}
